using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestAotMorph
{
    public partial class MainForm : Form
    {
        public MainForm(string[] args)
        {
            InitializeComponent();
            if (args.Length == 0) {
                MessageBox.Show("Please specify AOT dictionaries path in the command line");
                SourceWordBox.Enabled = false;
                return;
            }
            if (!AotMorphLib.MorphLibDll.MorphoSetPath(args[0]))
                MessageBox.Show(String.Format("Invalid AOT dictionaries path {0}", args[0]));
        }

        private void GetBaseFormBtn_Click(object sender, EventArgs e)
        {
            if (SourceWordBox.Text == "")
                return;
            baseFormBox.Text = "";
            paradigmBox.Text = "";
            Cursor.Current = Cursors.WaitCursor;
            bool known;
            AotMorphLib.Morphology.Language lang = AotMorphLib.Morphology.Language.Unknown;
//            AotMorphLib.Morphology.PartOfSpeech partOfSpeech;
            List<string> forms;
            List<AotMorphLib.Morphology.PartOfSpeech> partsOfSpeech;
            if (AotMorphLib.Morphology.GetBaseForms(SourceWordBox.Text, out forms, out known, ref lang,
                out partsOfSpeech))
            {
                string[] baseForms = new string[forms.Count + 1];
                
                string baseForm = "*** Number of forms: ";
                baseForm += forms.Count + "; ";
                if (known)
                    baseForm += "known";
                else
                    baseForm += "predicted";
                baseForm += " word; ";
                string s = "unknown";
                switch (lang) {
                    case AotMorphLib.Morphology.Language.English:
                        s = "English";
                        break;
                    case AotMorphLib.Morphology.Language.German:
                        s = "German";
                        break;
                    case AotMorphLib.Morphology.Language.Russian:
                        s = "Russian";
                        break;
                }
                baseForms[0] = baseForm + s + " language ***";

                for (int i = 0; i < forms.Count; ++i)
                    baseForms[i + 1] = (i + 1) + ". " + forms[i] + " (" +
                        AotMorphLib.Morphology.GetPartOfSpeechName(partsOfSpeech[i]) + ")";

                baseFormBox.Lines = baseForms;

                List<List<string>> paradigmList;
                if (AotMorphLib.Morphology.GetParadigms(SourceWordBox.Text, out paradigmList, out known, ref lang,
                    out partsOfSpeech))
                {
                    string[] paradigms = new string[paradigmList.Count];
                    int i = 0;
                    foreach (List<string> wordParadigm in paradigmList) {
                        s = ".";
                        foreach (string paradigm in wordParadigm) {
                            s += ' ';
                            s += paradigm;
                        }
                        paradigms[i] = (i + 1) + s + " (" +
                            AotMorphLib.Morphology.GetPartOfSpeechName(partsOfSpeech[i]) + ")";
                        ++i;
                    }
                    paradigmBox.Lines = paradigms;
                }
                else
                    baseForm = "!error";
            }
            else {
                baseFormBox.Text = "!error";
                paradigmBox.Text = "";
            }
            Cursor.Current = Cursor;
        }

        private void SourceWordText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                GetBaseFormBtn_Click(null, null);
        }
    }
}